import os
class CodeTools:
    def __init__(self, codeEntity, codeName, extendsType, fatherClass, isclass):
        self.isclass = isclass
        self.fatherClass = fatherClass
        self.extendsType = extendsType
        self.codeName = codeName
        self.codeEntity = codeEntity

    def genrateCode(self):
        path = self.createFile()
        f = open(path + "/" + str(self.codeName).capitalize() + ".java", "w", encoding="utf-8")
        if self.fatherClass is not None and self.fatherClass != "":
            f.write("\n\npublic " + self.isclass + " " + str(self.codeName).capitalize() + " " + str(
                self.extendsType) + " " + str(self.fatherClass).capitalize() + "" + "{\n\n")
        else:  # 不需要继承
            f.write("\n\npublic " + self.isclass + " " + str(self.codeName).capitalize() + "{\n\n")
        if self.isclass=="class":
            for key in self.codeEntity:  # 这里的key是序号
                code = self.codeEntity[key]
                if code.tag!="":
                    f.write("  " + code.promise + " " + code.nameType + " " + code.name + " " + ";//" + code.tag + "\n")
                else:
                    f.write("  " + code.promise + " " + code.nameType + " " + code.name + " " + ";"+"\n")
            for key in self.codeEntity:
                code = self.codeEntity[key]
                f.write("\n  public " + code.promise + " get" + str(
                    code.name).capitalize() + "() {\n     return " + code.name + "\n  }\n")
                f.write("  public void set" + str(
                    code.name).capitalize() + "(" + code.nameType + " " + code.name + ") {\n    this." + code.name + "=" + code.name + ";\n  }\n")
        else:
            for key in self.codeEntity:  # 这里的key是序号
                code = self.codeEntity[key]
                if code.tag!="":
                    f.write("  "+ code.nameType + " " + code.name + " " + ";//" + code.tag + "\n")
                else:
                    f.write("  " + code.nameType + " " + code.name + " " + ";"+"\n")
        f.write("\n}\n")
        f.close()
        print('创建成功')

    def createFile(self):
        currentPath = os.getcwd()
        print(currentPath)
        path = currentPath + "/code/java"
        if not os.path.exists(path):
            print('创建文件夹')
            os.makedirs(path)
        return path
